begintownscript;

variables;
short creature_count = 0;
short choice = 0;
short pc_counter = 0;
short lead_char = 0;

body;

beginstate 0; //INIT_STATE

	add_range_to_group(6,24,1);
	set_level(1001,25);

	if((town_status(3) == 2) || (town_status(2) == 2))
		make_town_hostile(2);

	enable_add_chars(1);
	set_crime_tolerance(2);
	set_name(18, "Charles");
	set_level(18, 5);
	set_name(22, "Lieutenant Newmann");
	set_level(22,28);
	set_name(24, "Private Stirlen");
	set_level(24,18);

	//Check to see if Stirlen got transferred, or if he ran off
	if((get_flag(2,16) == 2) || (get_flag(20,0) == 1))
		erase_char(24);

	//re-open gates after visiting ruins
	if((get_flag(2,19) == 1) && (get_flag(2,20) == 0) && (get_flag(4,0) != 0)) {
		set_flag(2,0,0);
		set_flag(2,1,0);
		set_flag(2,2,0);
		set_flag(2,3,0);
		set_flag(2,4,0);
		set_flag(2,5,0);
		set_flag(2,20,1);
		}


	//set up the gates
	if(get_flag(2,0) == 1) {
		flip_terrain(23,43);
		flip_terrain(24,43);
		flip_terrain(25,43);
		}
	if(get_flag(2,1) == 1) {
		flip_terrain(23,4);
		flip_terrain(24,4);
		flip_terrain(25,4);
		}
	if(get_flag(2,2) == 1) {
		flip_terrain(42,23);
		flip_terrain(42,24);
		flip_terrain(42,25);
		}
	if(get_flag(2,3) == 1) {
		flip_terrain(23,40);
		flip_terrain(24,40);
		flip_terrain(25,40);
		}
	if(get_flag(2,4) == 1) {
		flip_terrain(23,7);
		flip_terrain(24,7);
		flip_terrain(25,7);
		}
	if(get_flag(2,5) == 1) {
		flip_terrain(39,23);
		flip_terrain(39,24);
		flip_terrain(39,25);
		}

//Walk through the gates after the blasts

	if((get_flag(2,6) == 1) && (get_flag(0,25) == 4)) {
		reset_dialog();
		add_dialog_str(0,"As you return to Fort Galima, you notice a definite change in the atmosphere. The tension here is so thick, you'd need a broadsword to cut it.",0);
		add_dialog_str(1,"Soldiers stare at you suspiciously as you pass, and the whole fort seems to be silent, save for the crackling torches that line the entrance.",0);
		if(get_flag(4,0) == 0) {
			add_dialog_str(2,"Strange. The soldiers don't seem willing to open the gate for you.",0);
			add_dialog_str(3,"After an awkward moment of waiting, one soldier addresses you curtly, saying _Fort Galima is currently under a lockdown, and will be until further notice._",0);
			}
		run_dialog(1);
		set_flag(2,6,2);
	}

//walk through the gates for the first time

	if(get_flag(2,6) == 0) {
		reset_dialog();
		add_dialog_str(0,"As you enter the torchlit gates of Fort Galima, you're immediately struck by how old and worn the stone appears. This fort must be incredibly old.",0);
		add_dialog_str(1,"Surprisingly, the walls show no signs of severe damage, except from unchecked moss. It seems that Fort Galima hasn't seen all that much action.",0);
		add_dialog_str(2,"The soldiers watching the gate barely seem to notice you. In fact, unless you're mistaken, one of them is sleeping.",0);
		add_dialog_str(3,"At best, this fort could be called poorly-staffed. Personally, you'd think of it more as a sitting duck.",0);
		run_dialog(1);
		set_flag(2,6,1);
	}

	//Dialogue pics
	set_char_dialogue_pic(22,1907,0);
	set_char_dialogue_pic(24,1957,0);

break;


beginstate 1; //EXIT_STATE

	//transfer Stirlen
	if(get_flag(2,16) == 1)
		set_flag(2,16,2);

break;


beginstate 2; //START_STATE
	pc_counter = 0;
	while(char_ok(pc_counter) == 0) {
		pc_counter = pc_counter + 1;
		}
	lead_char = pc_counter;

break;


beginstate 10; 

break;


beginstate 15; //Enter mess hall
	if(get_flag(2,7) == 0) {
		message_dialog("You enter the fort's main building, the ground floor of which seems to hold the mess hall. You expected it to be far louder than this, and are somewhat surprised to find just a few tired-looking, off-duty soldiers.","Again, this fort's preparedness proves itself to be underwhelming.");
		set_flag(2,7,1);
	}

break;


beginstate 20; //View of lake on west side of fort
	if(get_flag(2,12) == 0) {
		reset_dialog();
		if(get_flag(3,1) != 1) {
			add_dialog_str(0,"This side of the fort overlooks the entire lake, and you can even catch a glimpse of the falls at the far north, with that strange stone bridge spanning a stretch of them.",0);
			add_dialog_str(1,"From here, you can see just how far the fort is above the lake. It looks like a pretty sheer drop.",0);
			}
		else
			add_dialog_str(2,"This view from this part of the fort just doesn't compare to the one from upstairs.",0); 
	if((char_ok(24) == 1) && (party_can_see_loc(char_loc_x(24),char_loc_y(24)) == 1))
		add_dialog_str(3,"You can only see one soldier over here... and you can't help but wonder why they're bothering. Someone would have to be a total idiot to attack this side of the fort.",0);
	else
		add_dialog_str(3,"No wonder there aren't any troops watching over here... someone would have to be a total idiot to attack this side of the fort.",0);
		run_dialog(1);
		set_flag(2,12,1);
	}
break;


beginstate 25; //Enter kitchen
	if(get_flag(2,8) == 0) {
		message_dialog("Entering the fort's kitchen, you are immediately assaulted by the smell of thin beef stew. A rather short man is frantically running around the room, occasionally shifting a bubbling pot around.","It smells a bit like something's burning.");
		set_flag(2,8,1);
	}
break;


beginstate 26; //Soup pot
	if(char_ok(18) == 1) 
		message_dialog("This pot is filled with what looks like bubbling beef stew. It smells like it too.","Before you can attempt to taste it, the cook rushes over and shoves you away from the pot.");
	else
		message_dialog("With the cook out of the way, you can finally taste the soup that's simmering in this pot. Leaning over to scoop some out, you nearly scald yourself.","In a disappointing conclusion, the soup actually doesn't taste all that great. It's just thin beef soup, probably from a military cookbook.");
break;


beginstate 27; //Ovens
	message_dialog("You reach into the oven, and promptly burn yourself.","That wasn't very smart of you, now was it?");
	damage_char(lead_char,5,1);
break;


beginstate 30; //approach well
	if(get_flag(2,9) == 0) {
		message_dialog("The fort's well looks like it hasn't been cleaned in weeks. Bits of dead leaves line the edges, and the bucket by the edge is a bit moldy.","");
		set_flag(2,9,1);
	}
break;


beginstate 35; //Practice field
	if(get_flag(2,10) == 0) {
		message_dialog("This corner of the fort was apparently dedicated to training and weapons practice. The dummies look like they're rotting off their poles, and the arrows stuck in them look rusty.","A few spots of blood testify to maneuvers gone wrong.");
		set_flag(2,10,1);
	}
break;

beginstate 36; //Blood spot on practice field
	if(get_flag(2,11) == 0) {
		message_dialog("Looks like someone was seriously injured here. The blood looks fairly fresh.","");
		set_flag(2,11,1);
	}
break;


//Stairs & ladders
beginstate 40; //Ladder, NW Tower
	if(is_combat() == 1) {
		block_entry(1);
		print_str_color("You can't climb the ladder in combat mode.",1);
		end();
		}
	move_to_new_town(3,10,7);
break;

beginstate 41; //Ladder, SW Tower
	if(is_combat() == 1) {
		block_entry(1);
		print_str_color("You can't climb the ladder in combat mode.",1);
		end();
		}
	move_to_new_town(3,10,40);
break;

beginstate 42; //Ladder, NE Tower
	if(is_combat() == 1) {
		block_entry(1);
		print_str_color("You can't climb the ladder in combat mode.",1);
		end();
		}
	move_to_new_town(3,40,7);
break;

beginstate 43; //Ladder, SE Tower
	if(is_combat() == 1) {
		block_entry(1);
		print_str_color("You can't climb the ladder in combat mode.",1);
		end();
		}
	move_to_new_town(3,40,40);
break;

beginstate 44; //Stairs, Central Tower
	if(is_combat() == 1) {
		block_entry(1);
		print_str_color("You can't climb the ladder in combat mode.",1);
		end();
		}
	move_to_new_town(3,28,17);
break;
//End of Stairs & ladders


beginstate 45; //Living Quarters
	if(get_flag(2,18) == 0) {
		reset_dialog();
		add_dialog_str(0,"These appear to be the soldiers' living quarters. The first thing you notice is that there don't seem to be enough beds for the number of troops you've seen around here.",0);
		add_dialog_str(1,"The second thing you notice is that the room is... well, _spartan_ is being too nice. The room is downright barren, empty of nearly any soldiers' personal effects, as well as general signs of recent use.",0);
		run_dialog(1);
		set_flag(2,18,1);
	}
break;


beginstate 50; //Armory
	if(get_flag(2,17) == 0) {
		message_dialog("Entering what appears to be the fort's armory, you're appalled by the crude weaponry which adorns the counter. You would've thought that the Empire would supply better equipment for their soldiers...","Then again, this might just be the spare equimpent. The soldiers probably have the real stuff. But either way, it's not an encouraging sign.");
		set_flag(2,17,1);
	}
break;


beginstate 60; //exit, shunted to cutscene town, etc, Hektos explodes


//IF YOU'VE MADE THE TOWN HOSTILE, YOU LOSE.

if((town_status(2) == 2) || (town_status(3) == 2)) {
	reset_dialog();
	add_dialog_str(0,"You dash from the fort, realizing that you probably cannot return again... in fact, you'll probably never be able to return to the province, either.",0);
	if(get_flag(3,4) != 1)
		add_dialog_str(1,"And to make matters worse, you never even completed your first quest...",0);
	add_dialog_str(2,"Fearing large-scale Empire retribution, you flee the province and hide out in the mountains for a few months, leaving Yris and its problems behind forever.",0);
	run_dialog(1);
	block_entry(1);
	end_scenario(0);
	}



if((get_flag(2,19) == 0) && (get_flag(3,4) == 1)) {

	//echoing rumble
	play_sound(-108);
	pause(4);
	play_sound(-108);
	pause(4);
	play_sound(-108);
	pause(4);

	reset_dialog();
	add_dialog_str(0,"As you move to leave the fort, you hear a low rumbling sound. You can't tell where it's coming from, but it sounds like a lot of troops within the fort have noticed. A series of confused yells reaches your ears.",0);
	add_dialog_str(1,"The rumblings cease for a moment, and you run into the woods to the west of the fort, whence the sounds came.",0);
	run_dialog(1);

	set_flag(2,19,1);

	//close gates until you've been to the ruins
	set_flag(2,0,1);
		flip_terrain(23,43);
		flip_terrain(24,43);
		flip_terrain(25,43);
	set_flag(2,1,1);
		flip_terrain(23,4);
		flip_terrain(24,4);
		flip_terrain(25,4);
	set_flag(2,2,1);
		flip_terrain(42,23);
		flip_terrain(42,24);
		flip_terrain(42,25);
	set_flag(2,3,1);
		flip_terrain(23,40);
		flip_terrain(24,40);
		flip_terrain(25,40);
	set_flag(2,4,1);
		flip_terrain(23,7);
		flip_terrain(24,7);
		flip_terrain(25,7);
	set_flag(2,5,1);
		flip_terrain(39,23);
		flip_terrain(39,24);
		flip_terrain(39,25);
	
	//Take them to the cutscene town
	move_to_new_town(7,25,17);

}

break;